package com.yomahub.liteflow.example.xstream.parameterFileDemo;

import cn.hutool.json.JSONUtil;
import com.thoughtworks.xstream.XStream;
import com.yomahub.liteflow.example.xstream.parameterFileDemo.base.*;

import java.util.*;

/**
 * @program: freemarker-project
 * @description:
 * @author: yyyyjinying
 * @create: 2023-11-09 21:55
 **/
public class ObjectToXmlClient {
    public static void main(String[] args) {
        ParameterFile parameterFile = new ParameterFile();
        Config config = new Config();
        Header header = new Header();
        header.setMemo("737-7译码配置xml文件");
        header.setSynch1("583");
        header.setSynch2("1464");
        header.setSynch3("2631");
        header.setSynch4("3512");
        header.setTitle("737-7");
        header.setVersion("1.0");
        header.setWordsize("1024");
        config.setHeader(header);
        SuperFrameCounter superFrameCounter = new SuperFrameCounter();
        superFrameCounter.setOlsb("9");
        superFrameCounter.setOmsb("8");
        superFrameCounter.setSubframe("7");
        superFrameCounter.setWord("499");
        config.setSuperFrameCounter(superFrameCounter);
        parameterFile.setConfig(config);
        ArrayList<Parameter> parameters = new ArrayList<>();
        Parameter parameter = new Parameter();
        parameter.setCode("900001");
        parameter.setInsertion("1");
        List<Location> locations = new ArrayList<>();
        Location location = new Location();
        location.setId("1");
        location.setOlsb("8");
        location.setOmsb("12");
        location.setSubframe("1");
        location.setSupframe("2");
        location.setWord("256");
        locations.add(location);
        parameter.setLocations(locations);
        parameter.setMemo("Date Time");
        parameter.setName("DATETIME");
        parameter.setRate("1");
        parameter.setSign("N");
        parameter.setType("DAT");
        parameter.setCode("900001");
        parameter.setInsertion("1");
        parameters.add(parameter);
        Info info = new Info();
        Map<String, Object> map = new HashMap<>();
        map.put("1", "H3");
        String strs = getStr("1", "2", "0");

        map.put("12", strs);
        map.put("11", "H32");

        info.setValuemap(getStrValue(map));
        parameter.setInfo(info);

        List<Expression> expressions = new ArrayList<>();
        Expression expression =new Expression();
        expression.setC1("999999");
        expression.setMaxRawValue("0.004880000");
        expression.setMinRawValue("999999");
        expressions.add(expression);
        parameter.setExpressions(expressions);
        parameterFile.setParameters(parameters);

        XStream xstream = new XStream();
        xstream.alias("parameterFile", ParameterFile.class);
        xstream.alias("parameter", Parameter.class);
        xstream.alias("location", Location.class);
        xstream.alias("expression", Expression.class);
        xstream.useAttributeFor(Header.class, "version");
        xstream.useAttributeFor(Header.class, "wordsize");
        xstream.useAttributeFor(Header.class, "synch1");
        xstream.useAttributeFor(Header.class, "synch2");
        xstream.useAttributeFor(Header.class, "synch3");
        xstream.useAttributeFor(Header.class, "synch4");
        xstream.useAttributeFor(Header.class, "title");
        xstream.useAttributeFor(Header.class, "memo");

        xstream.useAttributeFor(SuperFrameCounter.class, "word");
        xstream.useAttributeFor(SuperFrameCounter.class, "olsb");
        xstream.useAttributeFor(SuperFrameCounter.class, "omsb");
        xstream.useAttributeFor(SuperFrameCounter.class, "subframe");


        xstream.useAttributeFor(Parameter.class, "code");
        xstream.useAttributeFor(Parameter.class, "rate");
        xstream.useAttributeFor(Parameter.class, "name");
        xstream.useAttributeFor(Parameter.class, "sign");
        xstream.useAttributeFor(Parameter.class, "type");
        xstream.useAttributeFor(Parameter.class, "memo");
        xstream.useAttributeFor(Parameter.class, "insertion");
        xstream.useAttributeFor(Location.class, "omsb");
        xstream.useAttributeFor(Location.class, "supframe");
        xstream.useAttributeFor(Location.class, "word");
        xstream.useAttributeFor(Location.class, "olsb");
        xstream.useAttributeFor(Location.class, "id");
        xstream.useAttributeFor(Location.class, "subframe");
        xstream.useAttributeFor(Info.class, "valuemap");

        xstream.useAttributeFor(Expression.class, "minRawValue");
        xstream.useAttributeFor(Expression.class, "maxRawValue");
        xstream.useAttributeFor(Expression.class, "c1");

        String xmlStr = xstream.toXML(parameterFile);
        System.out.println(xmlStr);

        // Object o = xstream.fromXML(xmlStr);
        // System.out.println(o.toString());

        System.out.println(JSONUtil.toJsonStr(map));

        System.out.println("fdfd dfdf ".trim());
    }

    public static String getStr(String ...str){
        return String.join(" ", str);
    }

    public static String getStrValue(Map<String, Object> map){
        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            stringBuilder.append(entry.getKey());
            stringBuilder.append(":");
            stringBuilder.append(entry.getValue());
            stringBuilder.append(",");
        }
        return stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
    }



}
